//
//  YXYCustomTabBar.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/26.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit

class YXYCustomTabBar: UITabBar {
    
    var closure:(() -> ())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width: CGFloat = kWidth / 5
        let height: CGFloat = bounds.size.height
        
        var index: CGFloat = 0
        
        let addBtn: UIButton = {
            let button: UIButton = UIButton()
            button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
            button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
            button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
            button.addTarget(self, action: "clickAddBtn:", forControlEvents: UIControlEvents.TouchUpInside)
            return button
        }()
        
        // 添加加号按钮
        self.addSubview(addBtn)
        addBtn.frame = CGRectMake(width*2, 0, width, height)
        
        for subview in subviews {
            //print(subview)
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //print(subview)
                
                if index == 2 {
                    index++
                }
                let x: CGFloat = index * width
                let y: CGFloat = 0
                subview.frame = CGRectMake(x, y, width, height)
                
                index++
            }
        }
    }
    
    // 加号按钮的点击事件
    func clickAddBtn (sender:UIButton) {
        
        closure?()
    }
}
